package engineer.view.database.connect;

import fx.FX;
import javafx.application.Application;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import utils.Base;
import utils.titles.Title;
import mysql.Connection;

/**
 * Created by nuno-rafael-figueiredo on 23-03-2014.
 */
public class ConnectController extends BorderPane {
    public static final String KEY = "connections";

    private final Base base = new Base(Title.CAPITAL_CASE, Title.fromObject(this));

    @FXML
    private ComboBox<Connection> savedConnectionsComboBox;

    @FXML
    private TextField hostnameTextField;

    @FXML
    private TextField portTextField;

    @FXML
    private TextField userTextField;

    @FXML
    private PasswordField passwordField;

    @FXML
    private TextField schemaTextField;

    @FXML
    private Button connectButton;

    @FXML
    private Button cancelButton;

    @FXML
    private CheckBox saveCheckBox;

    public final Property<Connection> connectionProperty = new SimpleObjectProperty<>();

    private ObservableList<Connection> savedConnections;

    public ConnectController() {
        FX.load(this);
    }

    @FXML
    private void initialize(){
        connectButton.setOnAction(this::onSignIn);
        cancelButton.setOnAction(this::onCancel);

        savedConnections = base.getObservableList(KEY, Connection[].class);

        savedConnections.addListener((ListChangeListener<Connection>) change -> base.save(KEY, savedConnections));
        savedConnectionsComboBox.getItems().addAll(savedConnections);

        savedConnectionsComboBox.valueProperty().addListener((observableValue, o, after) -> {
            saveCheckBox.setSelected(false);

            if (after != null) {
                hostnameTextField.setText(after.getHostname());
                portTextField.setText("" + after.getPort());
                userTextField.setText(after.getUser());
                passwordField.setText(after.getPassword());
                schemaTextField.setText(after.getSchemaName());
            } else {
                hostnameTextField.setText("localhost");
                portTextField.setText("3306");
                userTextField.setText("root");
                passwordField.clear();
                schemaTextField.clear();
            }
        });

        if (savedConnections.size() > 0) savedConnectionsComboBox.getSelectionModel().selectFirst();
    }

    private void onSignIn(ActionEvent actionEvent) {
        Connection connection = new Connection(
                hostnameTextField.getText(),
                Integer.parseInt(portTextField.getText()),
                userTextField.getText(),
                passwordField.getText(),
                schemaTextField.getText()
        );

        connectionProperty.setValue(connection);

        if (saveCheckBox.isSelected())
            savedConnections.add(connection);

        FX.close(this);
    }

    private void onCancel(ActionEvent actionEvent) {
        FX.close(this);
    }

    public static class Launcher extends Application {

        @Override
        public void start(Stage primaryStage) throws Exception {

        }

        public static void main(String... args){
            launch(args);
        }
    }
}
